/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ru.staf.SnmpTopologyExplorer;

import java.util.Vector;
import java.io.File;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;

import org.snmp4j.smi.VariableBinding;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.Integer32;


/**
 *
 * @author aezhov
 */
public class FilePseudoAgent extends GenericAgent {
    File file = null;
    String name = "Новый агент";

    public FilePseudoAgent() {

    }

    public String toString() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setFilename(String filePath) throws ConfigurationException {
        file = new File(filePath);
        if (!file.exists())
            throw new ConfigurationException(Errors.INVALID_FILENAME);

        if (!file.canRead())
            throw new ConfigurationException(Errors.INSUFFICIENT_RIGHTS);
    }

    public FilePseudoAgent(String filePath) throws ConfigurationException {
        setFilename(filePath);
    }

    public VariableBinding[] walk(String OIDstring) throws java.io.FileNotFoundException,
        java.io.IOException
    {
        BufferedReader reader = new BufferedReader(new FileReader(file));
        String line = null;
        String responseOID = null;
        String type = null;
        String valueString = null;
        Vector<VariableBinding> vector = new Vector<VariableBinding>();

        while (true) {
            line = reader.readLine();
            if (line == null)
                break;

            if (line.startsWith(OIDstring, 0) || line.startsWith(OIDstring, 1)) {
                int index = line.indexOf(' ');
                if (index > -1) {
                    responseOID = line.substring(0, index);
                    valueString = line.substring(index);
                    index = valueString.indexOf(':');
                    type = valueString.substring(0, index);
                    valueString = valueString.substring(index+2);
                    index = type.lastIndexOf(' ');
                    type = type.substring(index+1);

                    if (type.equals("INTEGER")) {
                        OID oid = new OID(responseOID);
                        Integer32 var = new Integer32(Integer.parseInt(valueString));
                        VariableBinding vb = new VariableBinding(oid, var);
                        vector.add(vb);
                    } else {
                        continue;
                    }
                }
            }
        }

        VariableBinding[] a = new VariableBinding[vector.size()];
        return vector.toArray(a);
    }

    public void setSnmpVersion(String version) {
        
    }
}
